package com.example.demo.task;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;
import org.springframework.util.StopWatch;
import org.springframework.util.StringUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Auther Created by l.wang on 2018/12/26.
 */
public class DynamicExecteTask implements Runnable {
    private final Log logger = LogFactory.getLog(getClass());

    public Object clazz;
    public Method execute;
    public Object[] parameter;

    public void setParameter(Object... parameter) { this.parameter = parameter; }

    public void setClazz(Object clazz) { this.clazz = clazz; }

    public void setExecute(Method execute) {
        this.execute = execute;
    }

    @Override
    public void run() {
        try {
//            StopWatch stopWatch = new StopWatch();
            long startTime = System.currentTimeMillis();
//            stopWatch.start();
            if (StringUtils.isEmpty(parameter))
                execute.invoke(clazz);
            else
                execute.invoke(clazz,parameter);
//            stopWatch.stop();
//            logger.info(execute + " execution time is " + stopWatch.getTotalTimeMillis());
            logger.info(execute + " execution time is " + (System.currentTimeMillis() - startTime));
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
